#coding=utf-8

from hashlib import md5 as md5lib
import pymongo
from .dao import client
db = client.local

'''
    用户以及管理员，说白了就是可以进来的人，目前没有做权限管理的打算
'''

def buildPwd(pwd):
    '''这里的密码的放入格式是["md5","xxxxxxx"] or ["org":"xxxxxxx"]'''
    if pwd[0] == "md5":
        return pwd[1]
    if pwd[0] == "org":
        return md5lib(pwd[1]).hexdigest()

def login(email,pwd):
    users = list(db.users.find({"email":email,"pwd":buildPwd(pwd)}))
    if len(users) == 1:
        return users[0]
    else:
        return False

def create(email,pwd,nickname):
    pass

def changePwd(email,old_pwd,new_pwd):
    pass

def changeNickname(email,old_nick,new_nick):
    pass
